package com.szcinda.call;

import com.szcinda.call.service.*;
import com.szcinda.call.utils.PoiUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping({"orderDetail"})
public class OrderDetailController {
    private final OrderDetailService orderDetailService;
    private final OrderDetailAccountService orderDetailAccountService;

    public OrderDetailController(OrderDetailService orderDetailService, OrderDetailAccountService orderDetailAccountService) {
        this.orderDetailService = orderDetailService;
        this.orderDetailAccountService = orderDetailAccountService;
    }


    //  插入订舱信息
    @PostMapping({"insertBookingDetail"})
    public Result<String> insertBookingDetail(@RequestBody BookingOrderDetailInsertDto insertDto) {
        orderDetailService.insertByBooking(insertDto);
        return Result.success();
    }

    //  插入提单信息
    @PostMapping({"insertPickingDetail"})
    public Result<String> insertPickingDetail(@RequestBody PickingOrderDetailInsertDto insertDto) {
        orderDetailService.insertByPicking(insertDto);
        return Result.success();
    }

    // 登录
    @PostMapping("login")
    public Result<String> login(@RequestBody LoginDto loginDto) {
        orderDetailAccountService.login(loginDto);
        return Result.success();
    }

    // 查询
    @PostMapping("query")
    public PageResult<OrderDetailDto> query(@RequestBody OrderDetailQueryDto queryDto) {
        return orderDetailService.query(queryDto);
    }

    @GetMapping("export")
    public ResponseEntity<byte[]> export(@RequestParam(value = "bookingNumber", required = false) String bookNumber,
                                         @RequestParam(value = "pickingNumber", required = false) String pickingNumber,
                                         @RequestParam(value = "createTimeStart", required = false) String createTimeStart,
                                         @RequestParam(value = "createTimeEnd", required = false) String createTimeEnd,
                                         @RequestParam(value = "userName") String userName) {
        OrderDetailQueryDto queryDto = new OrderDetailQueryDto();
        queryDto.setBookingNumber(bookNumber);
        queryDto.setPickingNumber(pickingNumber);
        queryDto.setUserName(userName);
        if (!StringUtils.isEmpty(createTimeStart)) {
            queryDto.setCreateTimeStart(LocalDate.parse(createTimeStart));
        }
        if (!StringUtils.isEmpty(createTimeEnd)) {
            queryDto.setCreateTimeEnd(LocalDate.parse(createTimeEnd));
        }
        // 准备需要导出的数据
        List<OrderDetailDto> details = orderDetailService.queryExportData(queryDto);
        // 将数据导出成excel
        return PoiUtil.exportOrderDetail(details);
    }
}
